package com.fqr;

import java.io.File;
import java.io.IOException;

public class FtpDestination extends Destination {

	private String host;
	private String port = "21";
	private String user;
	private String pass;
	private String directory;
	private String filename;

	private String srcFilename;

	public String getHost() {
		if (host != null && host.trim().length() > 0)
			return host;
		else
			return getInfo("host");
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		if (port != null && port.trim().length() > 0)
			return port;
		else
			return getInfo("port");
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getDirectory() {
		if (directory != null && directory.trim().length() > 0)
			return directory;
		else
			return getInfo("directory");
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

	public String getFilename() {
		if (filename != null && filename.trim().length() > 0)
			return filename;
		else
			return getInfo("filename");
	}

	public String getDestinationFilename() {
		String destFilename = getFilename();
		if (destFilename == null || destFilename.trim().length() == 0)
			return srcFilename;
		
		return formatFilename(srcFilename, destFilename);
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getUser() {
		if (user != null && user.trim().length() > 0)
			return user;
		else
			return getInfo("user");
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPass() {
		if (pass != null && pass.trim().length() > 0)
			return pass;
		else
			return getInfo("pass");
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public void send(String src) throws IOException {
		this.srcFilename = src;
		FtpUtil ftp = new FtpUtil();
		ftp.connect(getHost(), Integer.parseInt(getPort()), getUser(),
				getPass());
		String filename = getDestinationFilename();
		ftp.ftpFile(src, filename == null ? new File(src).getName() : filename,
				getDirectory());
		ftp.closeFtp();
	}

	public static void main(String[] args) {
		String name = "{yyyyMMdd}[now-1d]_aaa.csv";
		FtpDestination d = new FtpDestination();
		d.setFilename(name);
		System.out.println(d.getDestinationFilename());
	}

}
